package com.liaoye.hellocustomview.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.liaoye.hellocustomview.R;

public class PathGetTanView extends View {


    private Paint paint;
    private Path path;
    private final String TAG = PathGetTanView.class.getSimpleName();
    private Path dst;
    private PathMeasure pathMeasure;
    private float animatedValue;
    private float[] pos = new float[2];
    private float[] tan = new float[2];
    private Bitmap bitmap;

    public PathGetTanView(Context context) {
        super(context);
        init();
    }

    public PathGetTanView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arraw);



        //设置画笔的基本属性
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.RED);
        //设置填充样式
        paint.setStyle(Paint.Style.STROKE);
        //设置画笔宽度
        paint.setStrokeWidth(5);

        path = new Path();
        pathMeasure = new PathMeasure();
        dst = new Path();


        path.addCircle(0,0, 100, Path.Direction.CW);

        pathMeasure.setPath(path, false);




        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(animation -> {
            animatedValue = (float) animator.getAnimatedValue();
            invalidate();
        });
        animator.setDuration(2000);
        animator.start();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(200, 200);

        dst.reset();
        float length = pathMeasure.getLength();
        float start = 0;
        float stop = animatedValue * length;
        pathMeasure.getSegment(start, stop, dst, true);
        canvas.drawPath(dst, paint);

        float degrees  = (float) ((Math.atan2(tan[1], tan[0])) * 180 / Math.PI);

        //绘制箭头
//        pathMeasure.getPosTan(stop, pos, tan);
//        Matrix matrix = new Matrix();
//        matrix.postRotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
//        matrix.postTranslate(pos[0] - bitmap.getWidth() / 2, pos[1] - bitmap.getHeight() / 2);
//        canvas.drawBitmap(bitmap, matrix, paint);

        Matrix matrix = new Matrix();
        pathMeasure.getMatrix(stop, matrix, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
        matrix.preRotate(degrees, -bitmap.getWidth() / 2, -bitmap.getHeight() / 2);
        canvas.drawBitmap(bitmap, matrix, paint);


    }
}
